﻿using NAudio.Wave;
using QServices;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace QControlCenter
{
	public static  class AlarmHelper
	{
		public static Task? AlarmTask { get; set; }
		public static bool IsStop { get; set; }
		public static object lockobj = new object();
		public static void StopAlarm()
		{
			IsStop = true;
		}
		
		public static void StartAlarm()
		{
			lock (lockobj)
			{
				if (AlarmTask != null && AlarmTask.Status == TaskStatus.Running) return;
				if (string.IsNullOrWhiteSpace(AppRuntime.Alarm)) return;
				if (IsStop) return;
				IsStop = false;
				AlarmTask = Task.Run(() =>
				{
					while (true)
					{

						using var audioFile = new AudioFileReader(AppRuntime.Alarm);

						using var outputDevice = new WaveOutEvent();
						outputDevice.Init(audioFile);
						outputDevice.Volume = 1.0f;
						outputDevice.Play();
						while (outputDevice.PlaybackState == PlaybackState.Playing)
						{
							if (IsStop) break;
							Thread.Sleep(1000);
						}
						Thread.Sleep(1000);
						if (IsStop) break;
					}
				});
			}
		}

		
	}
}
